package com.guigu.controller;

import com.guigu.dto.diseaseDTO;
import com.guigu.dto.gyDTO;
import com.guigu.entity.*;
import com.guigu.exception.SsmException;
import com.guigu.service.diseaseService;
import com.guigu.vo.CodeGenerator;
import com.guigu.vo.Result;
import com.guigu.vo.diseaseVO;
import com.guigu.vo.estimateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("disease")
public class diseaseController {

    @Autowired
    private diseaseService diservice;
    @PostMapping("/query")
    public Result query(@RequestBody diseaseDTO dto){
        try{
            List<diseaseVO> list=this.diservice.query(dto);
            System.err.println(dto);
            if(!list.isEmpty()){
                return new Result<List<diseaseVO>>("200", "true", "查询成功", list);
            }else if (dto.getName()!=null){
                return new Result<List<diseaseVO>>("200", "true", "没有对应的数据", list);
            }
            else{
                return new Result<List<diseaseVO>>("400", "false", "查询失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new SsmException("异常");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody disease di){
        try{

            Integer id=this.diservice.maxid();

            // 如果id为null，将其默认设置为0
            if (id==null) {
                id = 0;
            }
            //生成根据id编码
            CodeGenerator generator = new CodeGenerator("JB");
            String newCode = generator.generateCode(id+1);
            System.out.println(newCode);
            di.setCodes(newCode);
            System.err.println(di);
            int id2=this.diservice.add(di);
            if(id2==1){
                return new Result<>("200", "true", "添加成功", null);
            }else{
                return new Result<>("400", "false", "添加失败", null);
            }

        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody disease di){
        try{
            int id=this.diservice.update(di);
            if(id==1){
                return new Result<>("200", "true", "修改成功", null);
            }else{
                return new Result<>("400", "false", "修改失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }

    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable int id){
        try{
            int i= this.diservice.delete(id);
            if(i==1){
                return new Result<>("200", "true", "删除成功", null);
            }else {
                return new Result<>("400", "false", "删除失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }


    @PostMapping("/querygy")
    public Result querygy(@RequestBody gyDTO dto){
        try {
            List<intervention> list = this.diservice.querygy(dto);
            System.err.println(dto);
            if (!list.isEmpty()) {
                System.out.println(list);
                return new Result<>("200", "true", "查询成功", list);
            } else {
                return new Result<List<intervention>>("400", "false", "查询失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SsmException("异常");
        }
    }

    @PostMapping("/queryyd")
    public Result queryyd(@RequestBody gyDTO dto){
        try {
            List<t_motionsportsevents> list = this.diservice.queryyd(dto);
            System.err.println(dto);
            if (!list.isEmpty()) {
                System.out.println(list);
                return new Result<>("200", "true", "查询成功", list);
            } else {
                return new Result<List<intervention>>("400", "false", "查询失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SsmException("异常");
        }
    }

    @PostMapping("/queryss")
    public Result queryss(@RequestBody gyDTO dto){
        try {
            List<t_meals_meal> list = this.diservice.queryss(dto);
            System.err.println(dto);
            if (!list.isEmpty()) {
                System.out.println(list);
                return new Result<>("200", "true", "查询成功", list);
            } else {
                return new Result<List<intervention>>("400", "false", "查询失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SsmException("异常");
        }
    }

}
